home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1999 November / Macworld (1999-11).dmg / Updaters / WhiteCap 3.0.4 / WhiteCap Source.sit / WhiteCap Source / Common / io / CEgIStream.cpp < prev    next >
C/C++ Source or Header  |  1999-07-16  |  6KB  |  372 lines

  1. #include "CEgIStream.h"
  2.  
  3. //#include <string.h>
  4.  
  5.  
  6. UtilStr CEgIStream::sTemp;
  7.  
  8.  
  9.  
  10. CEgIStream::CEgIStream( unsigned short int inBufSize ) {
  11.     mReadBufSize    = inBufSize;
  12.     mIsTied            = false;
  13.     mBufPos            = 0;
  14.     mPos            = 0;
  15.     
  16.     Wipe();
  17. }
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28. long CEgIStream::GetLong() {        
  29.     register unsigned long c, n = GetByte();
  30.     
  31.     c = GetByte();
  32.     n = n | ( c << 8 );
  33.     
  34.     c = GetByte();
  35.     n = n | ( c << 16 );
  36.     
  37.     c = GetByte();
  38.     
  39.     return (long) (n | ( c << 24 ));
  40. }
  41.  
  42.  
  43. float CEgIStream::GetFloat() {
  44.     long v = GetLong();
  45.     return *( (float*) &v );
  46. }
  47.  
  48.  
  49.  
  50.  
  51. short int CEgIStream::GetShort() {
  52.     register unsigned long n = GetByte();
  53.     
  54.     return (short int) ((GetByte() << 8) | n);
  55. }
  56.  
  57.  
  58.  
  59.  
  60.  
  61. unsigned char CEgIStream::GetByte() {
  62.     register unsigned char c;
  63.     
  64.     if ( mIsTied ) {
  65.         if ( mPos != 0 ) {
  66.             c = *((unsigned char*) mNextPtr);
  67.             mNextPtr++;
  68.             mPos++; }
  69.         else
  70.             throwErr( cTiedEOS ); }
  71.     else if ( mPos < mBufPos + mStrLen && mPos >= mBufPos ) {
  72.         c = *((unsigned char*) mNextPtr);
  73.         mNextPtr++;
  74.         mPos++; }
  75.     else if ( noErr() ) {
  76.         fillBuf();
  77.         c = GetByte();
  78.     }
  79.     
  80.     return c;
  81. }
  82.  
  83.  
  84.  
  85. unsigned char CEgIStream::PeekByte() {
  86.     register unsigned char c;
  87.     
  88.     if ( mIsTied ) {
  89.         if ( mPos != 0 )
  90.             c = *((unsigned char*) mNextPtr); }
  91.     else if ( mPos < mBufPos + mStrLen && mPos >= mBufPos ) 
  92.         c = *((unsigned char*) mNextPtr);
  93.     else if ( noErr() ) {
  94.         fillBuf();
  95.         if ( noErr() )
  96.             c = PeekByte();
  97.         else
  98.             throwErr( cNoErr );
  99.     }
  100.     
  101.     return c;
  102. }
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110. /*
  111. float CEgIStream::GetFloat() {
  112.     //ah!
  113. }
  114.  
  115.  
  116.  
  117. double CEgIStream::GetDbl() {
  118.     ah!
  119. }
  120. */
  121.  
  122.  
  123. void CEgIStream::Readln( UtilStr* outStr ) {
  124.     unsigned char p, c = GetByte();
  125.     
  126.     if ( outStr ) {
  127.         outStr -> Wipe();
  128.                 
  129.         while ( noErr() && c != 13 && c != 10 ) {        // Stop on a CR or LF or error
  130.             outStr -> Append( (char) c );
  131.             c = GetByte();
  132.         }
  133.         
  134.         p = PeekByte();
  135.         if ( ( p == 13 && c == 10 ) || ( p == 10 && c == 13 ) ) 
  136.             GetByte();
  137.     }
  138. }
  139.  
  140.  
  141.  
  142.  
  143. void CEgIStream::Readln() {
  144.     unsigned char p, c = GetByte();
  145.             
  146.     while ( noErr() && c != 13 && c != 10 )         // Stop on a CR or LFor error
  147.         c = GetByte();
  148.         
  149.     p = PeekByte();
  150.     if ( ( p == 13 && c == 10 ) || ( p == 10 && c == 13 ) ) 
  151.         GetByte();
  152.  
  153. }
  154.  
  155.  
  156.  
  157. unsigned char CEgIStream::GetByteSW() {
  158.     unsigned char c = GetByte();
  159.  
  160.     while ( noErr() && (c == 13 || c == 10 || c == 32 || c == 9) )
  161.         c = GetByte();
  162.  
  163.     return c;
  164. }
  165.  
  166.  
  167. bool CEgIStream::Read( UtilStr& outStr ) {
  168.     outStr.Wipe();
  169.         
  170.     unsigned char c = GetByteSW();
  171.                 
  172.     while ( noErr() && c != 13 && c != 9 && c != 32 && c != 10 ) {    // Stop on a CR, LF, TAB, space or error
  173.         outStr.Append( (char) c );
  174.         c = GetByte();
  175.     }
  176.     return c == 13 || c == 10;
  177. }
  178.  
  179.  
  180.  
  181. void CEgIStream::Read() {
  182.     unsigned char c = GetByteSW();
  183.                 
  184.     while ( noErr() && c != 13 && c != 9 && c != 32 && c != 10 )         // Stop on a CR, LF, space or error
  185.         c = GetByte();
  186. }
  187.  
  188.  
  189.  
  190.  
  191. float CEgIStream::ReadFloat() {
  192.     Read( sTemp );
  193.     return ((float) sTemp.GetValue( 100000 )) / 100000.0;
  194. }
  195.  
  196.  
  197. long CEgIStream::ReadInt() {
  198.     Read( sTemp );
  199.     return sTemp.GetValue();
  200. }
  201.  
  202.         
  203.         
  204. void CEgIStream::Read( UtilStr& outStr, unsigned long inBytes ) {
  205.     outStr.Assign( NULL, inBytes );
  206.     GetBlock( outStr.getCStr(), inBytes );
  207. }
  208.  
  209.  
  210.  
  211. void CEgIStream::ReadNumber( UtilStr& outStr ) {
  212.  
  213.     outStr.Wipe();
  214.     char c = GetByteSW();
  215.     
  216.     while ( noErr() && ( c == '.' || (c >= '0' && c <= '9') ) ) {
  217.         outStr.Append( c );
  218.         c = GetByte();
  219.     }
  220. }
  221.  
  222.  
  223.  
  224.  
  225.  
  226. bool CEgIStream::AssertToken( const char* inStr ) {
  227.     char c = GetByteSW();
  228.     
  229.     // Check first byte
  230.     if ( *inStr != c || ! noErr() )
  231.         return false;
  232.     inStr++;
  233.  
  234.     // Make sure following bytes in the stream match inStr 
  235.     while ( *inStr ) {
  236.         c = GetByte();
  237.         if ( *inStr != c || ! noErr() )
  238.             return false;
  239.         inStr++;
  240.     }
  241.     
  242.     return true;
  243. }
  244.  
  245.  
  246.  
  247. long CEgIStream::GetBlock( void* destPtr, unsigned long inBytes ) {
  248.     long bytesRead = inBytes;
  249.         
  250.     if ( mIsTied ) {
  251.         if ( - mPos >= inBytes ) 
  252.             UtilStr::Move( destPtr, mNextPtr, bytesRead );
  253.         else {
  254.             bytesRead = 0;
  255.             throwErr( cTiedEOS ); 
  256.         } }
  257.     else if ( mPos >= mBufPos && mPos + bytesRead <= mBufPos + mStrLen )
  258.         UtilStr::Move( destPtr, mNextPtr, bytesRead );
  259.     else
  260.         fillBlock( mPos, destPtr, bytesRead );
  261.         
  262.     mPos        += bytesRead;
  263.     mNextPtr    += bytesRead;
  264.     
  265.     return bytesRead;
  266. }
  267.  
  268.  
  269.  
  270. void CEgIStream::fillBuf() {
  271.     long bytes = mReadBufSize;
  272.     
  273.     Dim( bytes );
  274.     mNextPtr = getCStr();
  275.     mBufPos = mPos;
  276.     if ( length() < bytes )                    // Verify that we dimmed ok
  277.         bytes = length();
  278.     fillBlock( mPos, getCStr(), bytes );
  279.     if ( bytes <= 0 )
  280.         throwErr( cEOSErr );
  281.     mStrLen = bytes;                        // Our str len could have only gotten shorter
  282. }
  283.  
  284.  
  285.  
  286. void CEgIStream::fillBlock( unsigned long, void*, long& ioBytes ) {
  287.     ioBytes = 0;
  288.  
  289. }
  290.     
  291.  
  292.  
  293. void CEgIStream::invalidateBuf() {
  294.  
  295.     Wipe();
  296. }
  297.  
  298.  
  299.  
  300.  
  301. void CEgIStream::skip( long inBytes ) {
  302.  
  303.     mPos        += inBytes;
  304.     mNextPtr    += inBytes;
  305. }
  306.  
  307.  
  308.  
  309.  
  310. void CEgIStream::ResetBuf() {
  311.     throwErr( cNoErr );
  312.     
  313.     mIsTied        = false;
  314.     mNextPtr    = getCStr();
  315.     mBufPos        = 0;
  316.     mPos        = 0;
  317. }
  318.  
  319.  
  320. void CEgIStream::Assign( const UtilStr& inSrce ) {
  321.     Assign( inSrce.getCStr(), inSrce.length() );
  322. }
  323.  
  324.                                         
  325. void CEgIStream::Assign( void* inSource, long inBytes ) {
  326.     UtilStr::Assign( inSource, inBytes );
  327.     ResetBuf();
  328. }
  329.  
  330.  
  331. void CEgIStream::Assign( CEgIStream* inSource, long inBytes ) {
  332.     
  333.  
  334.     if ( inSource ) {
  335.         Dim( inBytes );
  336.         if ( length() < inBytes )
  337.             inBytes = length();
  338.         inSource -> GetBlock( getCStr(), inBytes );
  339.     }
  340.     
  341.     ResetBuf();
  342. }
  343.  
  344.  
  345.  
  346. void CEgIStream::Tie( const UtilStr* inSource ) {
  347.  
  348.     Tie( inSource -> getCStr(), inSource -> length() );
  349. }
  350.  
  351.  
  352.  
  353.  
  354. void CEgIStream::Tie( const char* inSrce, long inNumBytes ) {
  355.     
  356.     throwErr( cNoErr );
  357.     mIsTied        = true;
  358.     mPos        = - inNumBytes;            // When mPos reaches zero, we're at tied end of stream
  359.     mNextPtr    = inSrce;                // Set up our data ptr
  360.     
  361.     // If -1 was passed in thru inNumBytes, must calc the length of the C str.
  362.     if ( inNumBytes < 0 ) {
  363.         mPos = 0;
  364.         while ( *inSrce ) {
  365.             mPos--;
  366.             inSrce++;
  367.         }
  368.     }
  369.     
  370.     if ( ! mNextPtr )
  371.         mPos = 0;
  372. }